package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.PublicNotice;
import com.rfid.project.service.PublicNoticeService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;

@Controller
public class PublicNoticeController {
	
	private static final Logger logger = LoggerFactory.getLogger(PublicNoticeController.class);
	
	@Autowired
	private PublicNoticeService publicNoticeService;
	
	
	@Autowired
	private PeopleService peopleService;
	

	@RequestMapping(value = "/publicNoticemgr")
	public String commonMilestonemgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "publicNoticemgr";
	}

	@RequestMapping(value = "/publicNotice")
	public String publicNotice(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People>peoples = peopleService.findAll();
		model.addAttribute("peoples", peoples);
		return "publicNotice";
	}
	
	@RequestMapping(value = "/queryPublicNotice")
	public String queryPublicNotice(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "content", defaultValue = "") String content,
			@RequestParam(value = "publishTimeEnd", defaultValue = "") Date publishTimeEnd,
			@RequestParam(value = "publishTimeBegin", defaultValue = "") Date publishTimeBegin,
			@RequestParam(value = "publisherId", defaultValue = "-1") Long publisherId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<PublicNotice> publicNotices = publicNoticeService.findAll(title, content, publishTimeBegin, publishTimeEnd, publisherId, pageNumber, pageSize, sort, order);
				
		JSONObject object = new JSONObject();
		object.put("total", publicNotices.getTotalElements());
		JSONArray models = new JSONArray();
		for (PublicNotice item : publicNotices) {
			JSONObject mod = new JSONObject();
			
			wrapData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findPublicNotice")
	public String findPublicNotice(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model) {
		PublicNotice publicNotice = publicNoticeService.findOne(id);
		JSONObject result = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(publicNotice, mod);
		result.put("success", true);
		result.put("data", mod);
		PageJsonBean.printObject(response, result);
		return null;
	}

	private void wrapData(PublicNotice item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("title", item.getTitle());
		mod.put("content", item.getContent());	
		mod.put("publishTime", com.rfid.project.util.DateUtil
				.dateTheDayToString(item.getPublishTime()));			
		
		People publisher_1 = item.getPublisher();
		if(null!=publisher_1){
		mod.put("publisherId", item.getPublisher().getId());
		mod.put("publisherName", item.getPublisher().getName());}
		else
		{
			mod.put("publisherId", "-1");
			mod.put("publisherName", "");				
		}
	}

	@RequestMapping(value = "/editPublicNotice", method = RequestMethod.POST)
	public String editPublicNotice(HttpServletRequest request,
			HttpServletResponse response, @Valid PublicNotice publicNotice,
			Model model, RedirectAttributes redirectAttributes) {
		People publisher = publicNotice.getPublisher();
		if(null == publisher || null == publisher.getId()){
			publicNotice.setPublisher((People)request.getSession().getAttribute("currentUser"));
		}
		publicNoticeService.savePublicNotice(publicNotice);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delPublicNotice")
	public String delPublicNotice(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.publicNoticeService.deletePublicNotice(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception{
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
}

